package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.common.exception.BusinessException;
import com.yike.common.pagination.PageInfo;
import com.yike.common.pagination.Paging;
import com.yike.common.tool.LoginUtil;
import com.yike.common.tool.StringUtil;
import com.yike.user.entity.User;
import com.yike.user.mapper.UserMapper;
import com.yike.user.param.CustomerPageParam;
import com.yike.user.param.CustomerParam;
import com.yike.user.service.PlatformCustomerService;
import com.yike.user.vo.CustomerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平台用户 服务实现类
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@Service
public class PlatformCustomerServiceImpl implements PlatformCustomerService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean saveCustomer(CustomerParam customerParam) throws Exception {
        // 判断手机号是否已存在
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getMobile, customerParam.getMobile()));
        if (user != null) {
            throw new BusinessException("当前手机号已存在");
        }
        user = new User();
        BeanUtils.copyProperties(customerParam, user);
        user.setUserNo(StringUtil.random(32)); // 生成用户ID
        user.setCreateBy(LoginUtil.getUserId());
        userMapper.insert(user);
        return true;
    }

    @Override
    public boolean updateCustomer(CustomerParam customerParam) throws Exception {
        // 获取用户信息
        User user = userMapper.selectById(customerParam.getId());
        if (user == null) {
            throw new BusinessException("用户信息异常");
        }
        // 修改的时候防止手机号重复
        Integer checkNum = userMapper.selectCount(Wrappers.lambdaQuery(User.class)
                .eq(User::getMobile, customerParam.getMobile())
                .ne(User::getId, customerParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前手机号已存在");
        }
        BeanUtils.copyProperties(customerParam, user);
        user.setUpdateBy(LoginUtil.getUserId());
        userMapper.updateById(user);
        return true;
    }

    @Override
    public boolean deleteCustomer(Long id) throws Exception {
        // 获取用户信息
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BusinessException("用户信息异常");
        }
        userMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean batchDel(List<Long> ids) throws Exception {
        userMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public CustomerVo getCustomer(Long id) throws Exception {
        return userMapper.getCustomer(id);
    }

    @Override
    public Paging<CustomerVo> getCustomerPageList(CustomerPageParam customerPageParam) throws Exception {
        Page<CustomerVo> page = new PageInfo<>(customerPageParam);
        IPage<CustomerVo> iPage = userMapper.getCustomerList(page, customerPageParam);
        return new Paging<CustomerVo>(iPage);
    }
}
